package com.education.admin.api.config;

import com.education.admin.api.interceptor.AuthInterceptor;
import com.education.common.interceptor.LogInterceptor;
import com.education.common.interceptor.ParamsValidateInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 余一
 * @Description
 * @date 2021/7/20 8:29
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private ParamsValidateInterceptor paramsValidateInterceptor;
    @Autowired
    private AuthInterceptor authInterceptor;
    @Autowired
    private LogInterceptor logInterceptor;
    @Value("${file.upload}")
    String uploadPath;

    //不需要拦截的请求路径
    private static final List<String> noInterceptorUrl= new ArrayList(){
        {
            add("/login");
            add("/image");
            add("/unAuth");
            add("/webSocket");
            add("/error");
            add("/uploads/**");
        }
    };

    /**
     * 配置静态资源路径
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //表示以/uploads 开头的所有Url全部指定本地磁盘路径 uploadPath
        //文件上传的虚拟路径
        registry.addResourceHandler("/uploads/**").addResourceLocations("file:" +
                uploadPath);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(paramsValidateInterceptor);
        registry.addInterceptor(logInterceptor);
        registry.addInterceptor(authInterceptor)
                .excludePathPatterns(noInterceptorUrl)
                .addPathPatterns("/**");
    }
}
